﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Controls;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Windows;
using System.Collections.ObjectModel;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    public class DominoViewModel : DataListViewModel
    {
        private UserControl gWindow;
        private ParentWindowViewModel gParentWindowViewModel;
        private DominoModel gDominoModel;
        

        public DominoViewModel(UserControl pWindow, ParentWindowViewModel pParentWindowViewModel)
        {
            gWindow = pWindow;
            gParentWindowViewModel = pParentWindowViewModel;
            gDominoModel = new DominoModel();

            UpdateList();
        }

        public void UpdateList()
        {
            DataList.Clear();

            List<WDominoType> typeList = WDominoType.GetAll();

            List<WDomino> list = WDomino.GetAll();
            for (int i = 0; i < list.Count; i++)
            {
                LDomino item = new LDomino();
                item.IsChoose = false;
                item.DominoUID = list[i].DominoUID;
                item.DominoTypeUID = list[i].DominoTypeUID;

                foreach(var type in typeList)
                {
                    if(type.DominoTypeUID == item.DominoTypeUID)
                    {
                        item.DominoTypeName = type.DominoTypeName;
                        item.DominoTypeSelected = item.DominoTypeName;
                    }
                }

                item.DominoStockPrice = list[i].DominoStockPrice;
                item.DominoSellPrice = list[i].DominoSellPrice;
                item.IsSold = list[i].IsSold;

                if(item.IsSold == 0)
                {
                    item.IsSoldStatus = "未售出";
                }
                else
                {
                    item.IsSoldStatus = "已售出";
                }

                DataList.Add(item);
            }
        }

        public ObservableCollection<LDomino> DataList
        {
            get
            {
                return gDominoModel.DataList;
            }

            set
            {
                DataList = value;
                RaisePropertyChanged("DataList");
            }
        }

        public ObservableCollection<LDomino> DataListEdit
        {
            get
            {
                return gDominoModel.DataListEdit;
            }

            set
            {
                DataListEdit = value;
                RaisePropertyChanged("DataListEdit");
            }
        }

        public Visibility DataListEditVisibility
        {
            get
            {
                return gDominoModel.dataListEditVisibility;
            }
            set
            {
                gDominoModel.dataListEditVisibility = value;
                RaisePropertyChanged("DataListEditVisibility");
            }
        }

        protected override void DLCloseCommand(object obj)
        {
            gParentWindowViewModel.RemoveUserControl(gWindow.Uid);
        }

        protected override void DLSaveCommand(object obj)
        {
            List<WDomino> list = new List<WDomino>();
            foreach (LDomino item in DataListEdit)
            {
                if (item.IsChoose)
                {
                    item.DominoTypeUID = WDominoType.GetDominoTypeUID(item.DominoTypeSelected);

                    if(item.SoldStatusSelected == "已售出")
                    {
                        item.IsSold = 1;
                    }
                    else
                    {
                        item.IsSold = 0;
                    }

                    list.Add(new WDomino(item));
                }
            }
            
            HttpResponse response = WDomino.Update(list);

            UpdateList();

            DataListEditVisibility = Visibility.Hidden;
            DLBSaveVisibility = Visibility.Hidden;
            EditMode = false;
        }

        protected override void DLQuitCommand(object obj)
        {
            gParentWindowViewModel.RemoveUserControl(gWindow.Uid);
        }

        protected override void DLGoCommand(object obj)
        {

        }

        protected override void DLPageUpCommand(object obj)
        {

        }

        protected override void DLPageDownCommand(object obj)
        {

        }

        protected override void DLCheckedCommand(object obj)
        {
            if (DLTIsChecked)
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = true;
                }
            }
            else
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = false;
                }
            }
        }

        protected override void DLAddCommand(object obj)
        {
            DominoInsert window = new DominoInsert();
            DominoInsertViewModel viewModel = new DominoInsertViewModel(window, this);
            window.DataContext = viewModel;
            window.ShowDialog();
        }

        protected override void DLEditCommand(object obj)
        {
            if(!EditMode)
            {
                DataListEdit.Clear();

                List<WDominoType> typeList = WDominoType.GetAll();

                foreach (LDomino item in DataList)
                {
                    if(item.IsChoose)
                    {
                        foreach(var type in typeList)
                        {
                            item.DominoTypes.Add(type.DominoTypeName);
                            if(item.DominoTypeUID == type.DominoTypeUID)
                            {
                                item.DominoTypeSelected = type.DominoTypeName;
                            }
                        }

                        item.SoldStatuses.Add("未售出");
                        item.SoldStatuses.Add("已售出");
                        if(item.IsSold == 0)
                        {
                            item.SoldStatusSelected = "未售出";
                        }
                        else
                        {
                            item.SoldStatusSelected = "已售出";
                        }

                        DataListEdit.Add(item);
                    }
                }

                if(DataListEdit.Count>0)
                {
                    DataListEditVisibility = Visibility.Visible;
                    DLBSaveVisibility = Visibility.Visible;
                    EditMode = true;
                }
            }
        }

        protected override void DLDeleteCommand(object obj)
        {
            List<WDomino> list = new List<WDomino>();
            foreach (LDomino item in DataList)
            {
                if (item.IsChoose)
                {
                    list.Add(new WDomino(item));
                }
            }

            HttpResponse response = WDomino.Delete(list);

            UpdateList();
        }
    }
}
